<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/3
 * Time: 23:33
 */
header('Content-type: application/json;charset=utf-8');
require_once(dirname(__FILE__) . '/../../inc/config.inc.php');
require_once(dirname(__FILE__) . '/../../function.php');
IsModelPriv('goodsorder');
$tbname = '#@__order';
//引入操作类
$field = isset($_POST['field']) ? $_POST['field'] : [];
$action = empty($_POST['action']) ? "" : htmlspecialchars($_POST['action']);

$wx_uid = $_SESSION['uid'];
$poster = "";
$op_uid="";
$post_arr = $dosql->GetOne("select `truename` from `#@__user` where id='$wx_uid' ");
if (is_array($post_arr) && isset($post_arr['truename'])) {
    $poster = $post_arr['truename'];
}


if(count($field)==0){
    $jssdk->return_json_body(['code'=>-1,'msg'=>'非法请求']);
}


if ($action == 'add') {

    $tasktype = empty($field['task_type']) ? 0 : intval($field['task_type']);
    $task = empty($field['task']) ? 0 : intval($field['task']);
    $crmid = isset($field['crmid']) ? intval($field['crmid']) : 0;


    $school = empty($field['school']) ? "" : htmlspecialchars($field['school']);
    $truename = empty($field['truename']) ? "" : htmlspecialchars($field['truename']);
    $mobile= empty($field['mobile']) ? "" : htmlspecialchars($field['mobile']);
    $mode= empty($field['mode']) ? "q" : htmlspecialchars($field['mode']);
    $content = empty($field['content']) ? "" : htmlspecialchars($field['content']);
    $manageids = empty($field['manageids']) ? "" : htmlspecialchars($field['manageids']);
    $beginTime = empty($field['beginTime']) ? "" : htmlspecialchars($field['beginTime']);
    $endTime = empty($field['endTime']) ? "" : htmlspecialchars($field['endTime']);
    $picarr= isset($field['picarr_arr']) ? json_encode($field['picarr_arr']) : "";


    // 工单信息入库
    $apply_type = htmlspecialchars($tasktype);
    $apply_device = htmlspecialchars($task);
    $jindu = "3";
    $latitude = "33.006170";
    $longitude = "114.015880";
    $accuracy = "14";
    $post_time = time();
    $post_time2 = date("Y-m-d H:i:s", time());
    $beginTime = strtotime($beginTime);
    $endTime = strtotime($endTime);


    $fee = "";
    //调取任务奖励
    if ($task > 0) {
        $fee_arr = $dosql->GetOne("SELECT * FROM `#@__task` WHERE id='$task'");
        if (is_array($fee_arr) && isset($fee_arr['id'])) {
            $fee = $fee_arr['value'];
        }
    }
    //调取 用户数据
    if ($crmid > 0) {
        $crm_arr = $dosql->GetOne("SELECT * FROM `#@__crm` WHERE  checkinfo='1' AND  id='$crmid' ");

        if (is_array($crm_arr) && isset($crm_arr['id'])) {
            $school = $crm_arr['school'];
            $address = $crm_arr['address'];
            $mobile = $crm_arr['mobile'];
            $truename = $crm_arr['truename'];
            if ($crm_arr['gps'] != "") {
                $gps_arr = explode(',', $crm_arr['gps']);

                if (is_array($gps_arr) && isset($gps_arr[1])) {
                    $latitude = $gps_arr[0];
                    $longitude = $gps_arr[1];
                }

            }
            $crm_uid = "";  //用户的 uid
            $crm_openid_str = "";
            if ($crm_arr['uid'] > 0) {
                $crm_uid = $crm_arr['uid'];
                $crm_openid = $dosql->GetOne("select openid  from `#@__user`  where id='$crm_uid'");
                if(is_array($crm_openid) && isset($crm_openid['openid']) ){
                    $crm_openid_str = $crm_openid['openid'];  //用户的 openid
                }

            }


        }
    } else {
        $jssdk->return_json_body(['code'=>-2,'msg'=>'没有选择客户，请选择']);
    }


    $remark_info = GetAuthor() . " 直接创建任务";


    if ($mode == 'p') {
        $op_op_name = GetAuthor() . ' 派单';
        //新建工单
        $sql2 = "insert into `#@__order`(openid,uid,school,truename,mobile,address,apply_type,apply_device,num,content,manageid,begintime,endtime,post_time,jindu,latitude,longitude,accuracy,poster,remark,remark_time,fee,crmid,is_accept,accept_time,mode,picfiles) values   ('$crm_openid_str','$crm_uid','$school','$truename','$mobile','$address','$apply_type','$apply_device','1','$content','$manageids','$beginTime','$endTime','$post_time','$jindu','$latitude','$longitude','$accuracy','$poster','$remark_info','$post_time','$fee','$crmid','1','$post_time','$mode','$picarr')";
        $dosql->ExecNoneQuery($sql2);
        $orderid = $dosql->GetLastID();
    }
    if ($mode == 'q') {
        $op_op_name = GetAuthor() . ' 发布抢单';
        //新建工单
        $sql2 = "insert into `#@__order`(openid,uid,school,truename,mobile,address,apply_type,apply_device,num,content,manageid,begintime,endtime,post_time,jindu,latitude,longitude,accuracy,poster,remark,remark_time,fee,crmid,is_accept,accept_time,mode,picfiles) values   ('$crm_openid_str','$crm_uid','$school','$truename','$mobile','$address','$apply_type','$apply_device','1','$content','','$beginTime','$endTime','$post_time','2','$latitude','$longitude','$accuracy','$poster','$remark_info','$post_time','$fee','$crmid','1','$post_time','$mode','$picarr')";
        $dosql->ExecNoneQuery($sql2);
        $orderid = $dosql->GetLastID();
    }

    $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','$op_op_name','$post_time','2','$wx_uid')");


    // 判断工单的 模式  q 抢单 p 派单

    if ($mode == 'p') {
        $ids_arr = explode(",", $manageids);
        //执行人
        $zhixingren = "";
        foreach ($ids_arr as $key => $val) {
            $zhixingren .= getManage($val) . "    ";
        }


        $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','系统已经派单','$post_time','3','$manageids')");

        //处理工单
        foreach ($ids_arr as $key => $val) {
            //获取工程师 openid
            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
            $openid_str = $m['openid'];
            //插入更新的状态




            $orderidname = date('Ymd', time()) . "-" . $orderid;
            //封装为json格式的数据
            $repairname = getRepairMode($apply_device);

            //$repairname = $repairname . "  奖励  " . $fee;


            $send_body['touser'] = $openid_str;
            $send_body['template_id'] = $cfg_neworder;
            $send_body['url'] = $cfg_weburl . 'wx/morderDetail.php?orderid=' . $orderid;
            $send_body['data']['first']['value'] = "📢 您好,系统下发新的工单需要您处理！";
            $send_body['data']['first']['color'] = "#FF0000";
            $send_body['data']['keyword1']['value'] = $orderidname;
            $send_body['data']['keyword1']['color'] = "#173177";
            $send_body['data']['keyword2']['value'] = $repairname;
            $send_body['data']['keyword2']['color'] = "#FF0000";
            $send_body['data']['keyword3']['value'] = $post_time2;
            //$send_body['data']['keyword3']['color'] = "#FF0000";
            $send_body['data']['keyword4']['value'] = "💗".$school."-".$truename;
            $send_body['data']['keyword4']['color'] = "#0e48cb";
            $send_body['data']['keyword5']['value'] = "⛳".$address;
            $send_body['data']['keyword5']['color'] = "#0e48cb";
            $send_body['data']['remark']['value'] = "👉请点击处理，如有特殊情况，请转单给其他工程师。服务热线:". $cfg_hotline."👈";
            $send_body['data']['remark']['color'] = "#66bf45";
            
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $g_AccessToken;
            $jssdk->https_curl_json($url, $send_body, 'json');
            $jssdk->return_json_body(['code'=>0,'msg'=>'添加成功']);


        }

    }

    // 抢单模式

    if ($mode == 'q') {
        //获取抢单的工程师
        $mode_user = [];
        $dosql->Execute("SELECT `openid` FROM  `#@__user`  WHERE `level`='3'   ORDER BY id asc");
        while ($row_qaing = $dosql->GetArray()) {
            $mode_user[] = $row_qaing['openid'];
        }


        //处理工单
        foreach ($mode_user as $key => $val) {
            //获取工程师 openid
            $openid_str = $val;
            $orderidname = date('Ymd', time()) . "-" . $orderid;
            //封装为json格式的数据
            $repairname = getRepairMode($apply_device);
            //$repairname = $repairname . "  奖励  " . $fee;
            $send_body['touser'] = $openid_str;
            $send_body['template_id'] = $cfg_neworder;
            $send_body['url'] = $cfg_weburl . 'wx/morderDetail.php?orderid=' . $orderid;
            $send_body['data']['first']['value'] = "📢 您好,您好，系统下发抢单任务成功。请处理";
            $send_body['data']['first']['color'] = "#FF0000";
            $send_body['data']['keyword1']['value'] = $orderidname;
            $send_body['data']['keyword1']['color'] = "#173177";
            $send_body['data']['keyword2']['value'] = $repairname;
            $send_body['data']['keyword2']['color'] = "#FF0000";
            $send_body['data']['keyword3']['value'] = $post_time2;
            //$send_body['data']['keyword3']['color'] = "#FF0000";
            $send_body['data']['keyword4']['value'] = "💗".$school."-".$truename;
            $send_body['data']['keyword4']['color'] = "#0e48cb";
            $send_body['data']['keyword5']['value'] = "⛳".$address;
            $send_body['data']['keyword5']['color'] = "#0e48cb";
            $send_body['data']['remark']['value'] = "👉请点击抢单，手慢无。服务热线:". $cfg_hotline."👈";
            $send_body['data']['remark']['color'] = "#FF0000";

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $g_AccessToken;
            $res=$jssdk->https_curl_json($url, $send_body, 'json');
            $jssdk->return_json_body(['code'=>0,'msg'=>'添加成功']);


        }

    }



}
